// 创建一个路由器，并暴露出去

// 第一步：引入路由器createRouter
import { createRouter, createWebHashHistory } from 'vue-router'

// 第二步：引入组件
import Home from '@/views/layout/Home.vue'
import Login from '@/views/system/Login.vue'

// 第三步：创建路由器
const router = createRouter({
    history: createWebHashHistory(), // 路由器工作模式，hash模式的路由
    routes: [  //配置路由规则
        { path: '/', component: Home, },

        { path: '/login', component: Login },

        {
            path: '/admin', component: () => import('@/views/layout/Admin.vue'),
            name: 'Admin',
            meta: { requiresAuth: true, title: '后台管理' },
            children: [
                // 工作空间
                { path: 'console', component: () => import('@/views/workSpace/Console.vue') },
                { path: 'monitoring', component: () => import('@/views/workSpace/Monitoring.vue') },
                // { path: 'analysis', component: () => import('@/views/Analysis.vue') },
                { path: 'system/personal', component: () => import('@/views/system/personal/index.vue') },
                // 系统管理
                { path: 'system/user', component: () => import('@/views/system/User.vue') },
                { path: 'system/role', component: () => import('@/views/system/Role.vue') },
                { path: 'system/address', component: () => import('@/views/system/Address.vue') },
                // 资源管理
                { path: 'resource', component: () => import('@/views/resource/index.vue') },
                { path: 'resource/totals', component: () => import('@/views/resource/Totals.vue') },
                // 参数管理
                { path: 'param', component: () => import('@/views/param/index.vue') },
                { path: 'price', component: () => import('@/views/price/index.vue') },
                // 物质量评估
                { path: 'wzlpg/support', component: () => import('@/views/wzlpg/support/index.vue') },
                { path: 'wzlpg/adjust', component: () => import('@/views/wzlpg/adjust/index.vue') },
                // 价值量评估
                { path: 'jzlpg/support', component: () => import('@/views/jzlpg/support/index.vue') },
                { path: 'jzlpg/adjust', component: () => import('@/views/jzlpg/adjust/index.vue') },
                // 字段维护
                { path: 'field/vegetationecologicalzone', component: () => import('@/views/field/VegetationEcologicalZone.vue') },
                { path: 'field/ecologicalengineeringzone', component: () => import('@/views/field/EcologicalEngineeringZone.vue') },
                { path: 'field/forestclass', component: () => import('@/views/field/ForestClass.vue') },
                { path: 'field/ecologicalzone', component: () => import('@/views/field/EcologicalZone.vue') },
                { path: 'field/treespecies', component: () => import('@/views/field/TreeSpecies.vue') },
                { path: 'field/origin', component: () => import('@/views/field/Origin.vue') },
                { path: 'field/agegroup', component: () => import('@/views/field/AgeGroup.vue') },

            ],
            redirect: '/admin/console'  // 重定向到控制台
        },
        {
            path: '/error',
            children: [
                { path: '401', component: () => import('@/views/error/401.vue') },
                { path: '403', component: () => import('@/views/error/403.vue') },
                { path: '404', component: () => import('@/views/error/404.vue') },
                { path: '500', component: () => import('@/views/error/500.vue') }
            ]
        },
        // 捕获所有未匹配的路由
        {
            path: '/:pathMatch(.*)*',
            redirect: '/error/404'
        }
        // 
    ]
})


// 设置路由守卫
router.beforeEach(async (to, from, next) => {
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
    if (requiresAuth) {
        try {
            // 动态导入 Pinia store，确保在应用初始化后使用
            const { useUserStore } = await import('@/store/user');
            const { loginUser } = useUserStore();
            // 检查用户是否登录
            if (!loginUser.username) {
                console.log("用户未登录，跳转登录页!");
                next('/login');
            } else {
                next();
            }

        } catch (error) {
            console.error('Pinia store 访问错误:', error);
            next('/login');
        }
    } else {
        next();
    }
});

// 第四步：暴露路由器router
export default router
